
#include <stdio.h>
#include <sqlite3.h>

static int
exec_callback(
    void *UNUSED,
    int argc,
    char **argv,
    char **azColName)
{
  int i = 0;

  for (i = 0; i < argc; ++i)
  {
    printf ("'%s' = '%s'\n", azColName[i], argv[i] ? argv[i] : "NULL");
  }
  printf ("\n");
  return 0;
}

int
main(
    int argc,
    char **argv)
{
  sqlite3 *db = NULL;
  char *zErrMsg = NULL;
  int rc = -1;

  if (3 != argc)
  {
    fprintf (stderr, "Usage: %s DATABASE SQL\n", argv[0]);
    return 1;
  }

  rc = sqlite3_open(argv[1], &db);
  if (rc)
  {
    const char* const errmsg = sqlite3_errmsg(db);
    fprintf (stderr, "Failed to open database '%s': %s\n", argv[1], errmsg);
    sqlite3_close (db);
    return 1;
  }

  rc = sqlite3_exec (db, argv[2], exec_callback, 0, &zErrMsg);
  if (SQLITE_OK != rc)
  {
    fprintf (stderr, "SQL error: %s\n\t%s", zErrMsg, argv[1]);
    sqlite3_free (zErrMsg);
  }

  sqlite3_close (db);
  return 0;
}

